package com.lz.football.module.trainingcamp
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.liangzhong.events.TrainingcampEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.trainingcamp.TrainingcampDataManager;
	import com.lz.football.module.club.item.BaseItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;

	/**
	 *训练类型item 
	 * @author Administrator
	 * 
	 */	
	public class TrainingTypeItem extends BaseItem
	{
		/**
		 *训练类型图标 
		 */
		private var icon:MovieClip;
		/**
		 * 训练等级
		 */
		private var trainingNumber:TextField;
		private var _useTime:int;
		private var _type:int;
		private var _buyTime:int;
		
		/**
		 *训练类型 
		 */
		private var _trainingType:int;
		/**
		 *训练费用  
		 */
		private var _cost:int;
		/**
		 * 
		 */
		private var trainingcost:TextField;
		
		/**
		 *等待训练冷却时间 
		 */
		private var time:TextField;
		/**
		 *清除冷却时间 
		 */
		private var powerBtn:SimpleButton;
		private var _supTime:int;
		
		/**
		 *是否开放此位置 
		 */
		private var _isOpen:Boolean;
		public function TrainingTypeItem()
		{
			super();
		}
		/**
		 * 
		 * @param view
		 * 
		 */
		override public function initUI(view:Object=null):void
		{
			_skin = Reflection.createInstance("com.lz.modul.trainingcamp.trainingItem");
			_skin.gotoAndStop(1);
			_skin.mc.gotoAndStop(1);
			_skin.mc.icon.gotoAndStop(1);
			this.addChild(_skin);
//			icon = _skin.icon;
//			trainingNumber = _skin.trainingNumber;
//			icon.gotoAndStop(1);
//			super.initUI();
			
		}
		
		/**
		 *设置训练类型 
		 * @param value
		 * 
		 */
		public function set trainingType(value:int):void
		{
			_trainingType = value;
			var clubLv:int = AppData.getInstance().clubDatamanager.clubInfo.level;
			_isOpen = false;
			switch(_trainingType)
			{
				case TrainingcampDataManager.ATTACK:
					if(clubLv < 10)
					{
						_skin.gotoAndStop(2);
						_skin.mc.des.text = "10级开放此训练位";
						return;
					}
					break;
				case TrainingcampDataManager.SPEED:
					if(clubLv < 12)
					{
						_skin.gotoAndStop(2);
						_skin.mc.des.text = "12级开放此训练位";
						return;
					}
					break;
				case TrainingcampDataManager.CONTROL:
					if(clubLv < 14)
					{
						_skin.gotoAndStop(2);
						_skin.mc.des.text = "14级开放此训练位";
						return;
					}
					break;
				case TrainingcampDataManager.POWER:
					if(clubLv < 16)
					{
						_skin.gotoAndStop(2);
						_skin.mc.des.text = "16级开放此训练位";
						return;
					}
					break;
				case TrainingcampDataManager.STEAL:
					if(clubLv < 18)
					{
						_skin.gotoAndStop(2);
						_skin.mc.des.text = "18级开放此训练位";
						return;
					}
					break;
				case TrainingcampDataManager.DEFENCE:
					if(clubLv < 20)
					{
						_skin.gotoAndStop(2);
						_skin.mc.des.text = "20级开放此训练位";
						return;
					}
					break;
			}
			_isOpen = true;
			_skin.gotoAndStop(1);
			super.initUI();
			_skin.mc.gotoAndStop(_trainingType);
			icon = _skin.mc.icon;
			icon.gotoAndStop(1);
			frame = _skin.mc.frame;
			trainingcost = _skin.mc.trainingcost;
			time = _skin.mc.bar.time;
			powerBtn = _skin.mc.bar.powerBtn;
			_skin.mc.bar.gotoAndStop(1);
			select = false;
			
			trainingcost.text = "0";
		}
		/**
		 * 移出 
		 * @param event
		 * 
		 */
		override public function onMouseRollOut(event:MouseEvent):void
		{
			if(_select)return;
			icon.gotoAndStop(1);
			super.onMouseRollOut(event);
		}
		
		/**
		 * 移上 
		 * @param event
		 * 
		 */
		override public function onMouseRollOver(event:MouseEvent):void
		{
			if(_select)return;
			icon.gotoAndStop(2);
			super.onMouseRollOver(event);
		}
		override public function set select(value:Boolean):void
		{
			super.select = value;
			if(value)
				icon.gotoAndStop(2);
			else
				icon.gotoAndStop(1);
		}
		
		override public function set tips(value:Object):void
		{
			switch(value.type)
			{
				case 1:
					super.tips = "提示：进攻训练，增加球员进攻属性的出现几率";
					break;
				case 2:
					super.tips = "提示：防守训练，增加球员防守属性的出现几率";
					trainingNumber.text= "3级开启";
					trainingNumber.textColor = 0xFF0000;
					break;
				case 3:
					super.tips = "提示：抢断训练，增加球员抢断属性的出现几率";
					trainingNumber.text= "6级开启";
					trainingNumber.textColor = 0xFF0000F;
					break;
				case 4:
					super.tips = "提示：力量训练，增加球员力量属性的出现几率";
					trainingNumber.text= "4级开启";
					trainingNumber.textColor = 0xFF0000;
					break;
				case 5:
					super.tips = "提示：速度训练，增加球员速度属性的出现几率";
					trainingNumber.text= "2级开启";
					trainingNumber.textColor = 0xFF0000;
					break;
				case 6:
					super.tips = "提示：控球训练，增加球员控球属性的出现几率";
					trainingNumber.text= "5级开启";
					trainingNumber.textColor = 0xFF0000;
					break;
			}
			_type = value.type ;
			removeEvtListener();
		}
		/**
		 *更新状态 
		 * 
		 */
		public function updataStatus($time:int):void
		{
			if(!_isOpen)return;//未开放
			_supTime = 7200 - (AppData.getInstance().time - $time);
			TimerManager.instance.removeFunction(onTiming);
			if(_supTime >0)//还有剩余时间
			{
				if(_skin.mc.bar != null)_skin.mc.bar.visible = true;
				removeEvtListener();
				powerBtn.addEventListener(MouseEvent.CLICK,onclearTimeHandler);
				TimerManager.instance.addFunction(onTiming);
			}else
			{
				if(_skin.mc.bar != null)_skin.mc.bar.visible = false;
				addEvtListener();
				TimerManager.instance.removeFunction(onTiming);
			}
		}
		/**
		 * 
		 * 
		 */
		private function onTiming():void
		{
			_supTime--;
			if(_supTime > 0)
			{
				time.text = DateUtil.formatShowHMS(_supTime,true) + "";
				
				var per:int = Math.ceil((_supTime / 7200) * 100);
				_skin.mc.bar.gotoAndStop(per);
			}else
			{
				_skin.mc.bar.visible = false;
				addEvtListener();
				TimerManager.instance.removeFunction(onTiming);
			}
		}
		
		protected function onclearTimeHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().clubDatamanager.clubInfo.cashpoint <10)
			{
//				Alert.showErrorTip("点券不足，不可以冷却！");
				Alert.noCashAlert("消耗10点卷可清除冷却时间。现有点券不足，是否前往充值？");
				return;
			}
			Alert.show("你确定花费10点券来清除冷却时间吗?","",Alert.NO,GlobalDef.AlertContainer,clearTime);
			
		}
		private function clearTime($type:int):void
		{
			if($type == Alert.YES)
				this.dispatchEvent(new TrainingcampEvent(TrainingcampEvent.CLEAR_UP_TIME,{type:type},true));
		}
		
		/**
		 *使用次数 
		 */
		public function get useTime():int
		{
			return _useTime;
		}

		/**
		 * @private
		 */
		public function set useTime(value:int):void
		{
			_useTime = value;
		}

		/**
		 *训练类型 
		 */
		public function get type():int
		{
			return _trainingType;
		}

		/**
		 *购买次数 
		 */
		public function get buyTime():int
		{
			return _buyTime;
		}

		/**
		 * @private
		 */
		public function set buyTime(value:int):void
		{
			_buyTime = value;
		}

		override public function destroyView():void
		{
			icon = null;
			
			
			super.destroyView();
		}

		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			if(powerBtn != null)powerBtn.removeEventListener(MouseEvent.CLICK,onclearTimeHandler);
			TimerManager.instance.removeFunction(onTiming);
		}
		/**
		 *训练费用  
		 */
		public function get cost():int
		{
			return _cost;
		}

		/**
		 * @private
		 */
		public function set cost(value:int):void
		{
			_cost = value;
			if(trainingcost != null)trainingcost.text = value + "";
		}

		/**
		 *剩余冷却时间 
		 */
		public function get supTime():int
		{
			return _supTime;
		}


		/**
		 * 点击 
		 * @param event
		 * 
		 */		
//		public function onMouseClick(event:MouseEvent):void
//		{
//			//			if(_select)return;
//			if(_callBack != null)_callBack.apply(null,[this]);
//		}
	}
}